﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services.DBContext;
using FukiERP.Services.Helper;

namespace FukiERP.Product
{
    public partial class frm_Product : Telerik.WinControls.UI.RadForm
    {
        List<Model> _models = null;
        List<ProductCategory> _productCate = null;
        int _currentID = 0;
        public frm_Product()
        {
            InitializeComponent();
        }

        private void frm_Product_Load(object sender, EventArgs e) {
            FukiERPDataContext context = DataAccess.GetDBContext();
            PresentHelper.LoadProductCate(context, drp_cate);
            LoadBrand(context);
            LoadModelBrand(context);
            // Load Models
            _models = context.Models.ToList();
            drp_Model.Text = CommonConstants.DefaultDropDownValue;
            LoadProductList(context);
        }

        private void LoadProductList(FukiERPDataContext context) {
            List<View_Product> products = context.View_Products.ToList();
            grd_ProductList.DataSource = products;
        }

        private void LoadBrand(FukiERPDataContext context) {
            List<ProductBrand> brands = context.ProductBrands.OrderBy(c => c.Name).ToList();
            drp_brand.DataSource = brands;
            drp_brand.DisplayMember = ColumName.Col_Name;
            drp_brand.Text = CommonConstants.DefaultDropDownValue;
        }

        private void LoadModelBrand(FukiERPDataContext context) {
            List<ModelBrand> mbrands = context.ModelBrands.OrderBy(c => c.Name).ToList();
            drp_ModelBrand.DataSource = mbrands;
            drp_ModelBrand.DisplayMember = ColumName.Col_Name;
            drp_ModelBrand.Text = CommonConstants.DefaultDropDownValue;
        }

        private void drp_ModelBrand_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e) {
            if (e.Position != -1 && _models != null && _models.Count > 0) {
                drp_Model.Items.Clear();
                ModelBrand mBrand = ((ModelBrand)drp_ModelBrand.Items[e.Position].Value);
                List<Model> models = _models.Where(c => c.ModelBrand == mBrand.ID || c.Name == CommonConstants.NoneValue).ToList();
                if (models != null && models.Count() > 0) {
                    drp_Model.DataSource = models;
                    drp_Model.DisplayMember = ColumName.Col_Name;
                }
                drp_Model.Text = CommonConstants.DefaultDropDownValue;
            }
        }

        private void btn_Create_Click(object sender, EventArgs e) {
            RefreshForm();
        }

        private void RefreshForm() {
            _currentID = 0;
            PresentHelper.ClearTextBox(tbx_ProCode, tbx_ProName, tbx_AnnoucePrice, tbx_AveragePrice, tbx_SalePrice);
            PresentHelper.ClearDropBox(drp_brand, drp_cate, drp_Model, drp_ModelBrand);
        }

        private void tbn_Save_Click(object sender, EventArgs e) {
            FukiERPDataContext context = DataAccess.GetDBContext();
            Services.DBContext.Product pro = null;
            errorProvider1.Clear();
            bool isValid = ValidateValue.ValidateRequirementGroup(errorProvider1, tbx_ProCode, tbx_ProName, tbx_SalePrice);
            isValid = isValid && ValidateValue.ValidateNumberGroup(errorProvider1, tbx_SalePrice,tbx_AnnoucePrice);
            isValid = isValid && ValidateValue.ValidateDropdownGroup(errorProvider1, drp_cate, drp_brand, drp_ModelBrand, drp_Model);
            if (!isValid) {
                MessageBox.Show(MessageConstants.GaneralValidateMessage);
                return;
            }
            if (_currentID == 0) {
                pro = new Services.DBContext.Product();
                context.Products.InsertOnSubmit(pro);
            } else {
                pro = context.Products.Where(c => c.ID == _currentID).FirstOrDefault();
            }
            if (pro == null) {
                return;
            }
            pro.Code = tbx_ProCode.Text;
            pro.Name = tbx_ProName.Text;
            pro.SalePrice = FukiERP.Common.Helper.CommonHelper.GetNumber(tbx_SalePrice.Text);
            pro.AnouncePrice = FukiERP.Common.Helper.CommonHelper.GetNumber(tbx_AnnoucePrice.Text);
            pro.CateID = (int) ((RadListDataItem)drp_cate.SelectedValue).Value;
            pro.ProductBrandID = ((ProductBrand)drp_brand.SelectedValue).ID;
            if (!string.IsNullOrEmpty(drp_ModelBrand.Text) && drp_ModelBrand.Text != CommonConstants.DefaultDropDownValue) {
                pro.ModelBrandID = ((ModelBrand)drp_ModelBrand.SelectedValue).ID;
            }
            if (!string.IsNullOrEmpty(drp_Model.Text) && drp_Model.Text != CommonConstants.DefaultDropDownValue) {
                pro.ModelID = ((Model)drp_Model.SelectedValue).ID;
            }
            context.SubmitChanges();
            LoadProductList(context);
        }

        private void btn_Delete_Click(object sender, EventArgs e) {
            if (_currentID != 0 && PresentHelper.ShowConfirmBox(MessageConstants.ConfirmDeleteMessage)) {
                FukiERPDataContext context = DataAccess.GetDBContext();
                Services.DBContext.Product pro = context.Products.Where(c => c.ID == _currentID).FirstOrDefault();
                if (pro != null) {
                    context.Products.DeleteOnSubmit(pro);
                    context.SubmitChanges();
                }
                MessageBox.Show(MessageConstants.DeleteSuccessMessage);
                RefreshForm();
                //LoadData();
            }
        }

        private void grd_Product_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e) {
            _currentID = int.Parse(grd_ProductList.Rows[e.RowIndex].Cells[0].Value.ToString());
            View_Product p = (View_Product)grd_ProductList.Rows[e.RowIndex].DataBoundItem;
            tbx_ProCode.Text = p.Code;
            tbx_ProName.Text = p.Name;
            tbx_AnnoucePrice.Text = p.AnouncePrice.ToString();
            tbx_SalePrice.Text = p.SalePrice.ToString();
            tbx_AveragePrice.Text = p.Cost.ToString();
            ProductCategory cate = _productCate.Where(c => c.ID == p.CateID).FirstOrDefault();
            if (cate != null) {
                int index = drp_cate.Items.Where(c => (int)((RadListDataItem)c.Value).Value == cate.ID).
                    FirstOrDefault().RowIndex;
                drp_cate.SelectedIndex = index;
                //drp_cate.Text = cate.Name;
            }

            ProductBrand brand = ((List<ProductBrand>)drp_brand.DataSource).
                Where(c => c.ID == p.ProductBrandID).FirstOrDefault();
            if (brand != null) {
                drp_brand.SelectedValue = brand;
                drp_brand.Text = brand.Name;
            }

            ModelBrand mbrand = ((List<ModelBrand>)drp_ModelBrand.DataSource).
                Where(c => c.ID == p.ModelBrandID).FirstOrDefault();
            if (mbrand != null) {
                drp_ModelBrand.SelectedValue = mbrand;
                drp_ModelBrand.Text = mbrand.Name;
            }

            drp_Model.Items.Clear();
            List<Model> models = _models.Where(c => c.ModelBrand == p.ModelBrandID || c.Name == CommonConstants.NoneValue).ToList();
            if (models != null && models.Count() > 0) {
                drp_Model.DataSource = models;
                drp_Model.DisplayMember = ColumName.Col_Name;
                drp_Model.Text = CommonConstants.DefaultDropDownValue;
                Model model = models.Where(c => c.ID == p.ModelID).FirstOrDefault();
                if (model != null) {
                    drp_Model.SelectedValue = model;
                    drp_Model.Text = model.Name;
                }
            }
        }
    }
}
